package 笔试.Shopee;

import java.util.Arrays;

public class 最小路径和 {
    /**
     * Note: 类名、方法名、参数名已经指定，请勿修改
     *
     * @param grid int整型 二维数组
     * @return int整型
     */
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m][n];
        dp[0][0] = grid[0][0];
        for (int i = 1; i < m; i++) {
            dp[i][0] = grid[i][0] + dp[i - 1][0];
        }
        for (int j = 1; j < n; j++) {
            dp[0][j] = grid[0][j] + dp[0][j - 1];
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                int dis1 = dp[i - 1][j];
                int dis2 = dp[i][j - 1];
                dp[i][j] = Math.min(dis1, dis2) + grid[i][j];
            }
        }
        for (int[] ints : dp) {
            System.out.println(Arrays.toString(ints));
        }
        return dp[m - 1][n - 1];
    }

    public static void main(String[] args) {
        最小路径和 solution = new 最小路径和();
        int[][] grid = {
                {1, 2, 3},
                {4, 5, 6}};
        System.out.println(solution.minPathSum(grid));
    }
}